package com.zby.admin.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zby.admin.pojo.ReturnReason;
import com.zby.admin.pojo.dto.ReturnReasonDto;
import com.zby.admin.pojo.vo.ReturnReasonVo;
import com.zby.admin.service.ReturnReasonService;
import com.zby.utils.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/reason")
public class ReturnReasonController {
    @Resource
    private ReturnReasonService returnReasonService;

    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ReturnReasonDto returnReasonDto, HttpServletRequest request) {
        // 判断是否登录
        Object userId = request.getSession().getAttribute("adminId");
        if (userId == null) {
            return Result.error(401, "用户未登录，请先登录");
        }
        boolean add = returnReasonService.add(returnReasonDto);
        if (!add) {
            return Result.error(500, "添加失败");
        }
        return Result.success("添加成功");
    }

    @PostMapping(value = "/update")
    public  Result<String> update(@RequestBody ReturnReasonDto returnReasonDto, HttpServletRequest request) {
        // 判断是否登录
        Object userId = request.getSession().getAttribute("adminId");
        if (userId == null) {
            return Result.error(401, "用户未登录，请先登录");
        }
        UpdateWrapper<ReturnReason> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", returnReasonDto.getId()).set("reason", returnReasonDto.getReason()).set("description", returnReasonDto.getDescription());
        boolean update = returnReasonService.update(updateWrapper);
        if (!update) {
            return Result.error(500, "修改失败");
        }
        return Result.success("修改成功");
    }

    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam()Integer id , HttpServletRequest request) {
        // 判断是否登录
        Object userId = request.getSession().getAttribute("adminId");
        if (userId == null) {
            return Result.error(401, "用户未登录，请先登录");
        }
        boolean delete = returnReasonService.removeById(id);
        if (!delete) {
            return Result.error(500, "删除失败");
        }
        return Result.success("删除成功");
    }

    @GetMapping(value = "/list")
    public Result<Page<ReturnReasonVo>> list(@RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "10") Integer pageSize,
                                             @RequestParam(required = false) String reasonTitle,
                                             HttpServletRequest request) {
        // 登录判断
        Object userId = request.getSession().getAttribute("adminId");
        if (userId == null) {
            return Result.error(401, "用户未登录，请先登录");
        }
        Page<ReturnReasonVo> pageInfo = returnReasonService.getAllReasonList(page, pageSize, reasonTitle);
        return Result.success(pageInfo);
    }
    @GetMapping(value = "/userList")
    public Result<List<ReturnReasonVo>> userList(HttpServletRequest request) {
//        // 登录判断
//        Object userId = request.getSession().getAttribute("userId");
//        Object adminId = request.getSession().getAttribute("adminId");
//        if (userId == null) {
//            return Result.error(401, "用户未登录，请先登录");
//        }
        List<ReturnReasonVo> list = returnReasonService.selectList();
        return Result.success(list);
    }
}
